#include "settingsdialog.h"
#include "ui_settingsdialog.h"

SettingsDialog::SettingsDialog(Settings *_settings, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsDialog)
{
    settings = _settings;
    ui->setupUi(this);

    switch(settings->window_close.type)
    {
        /* exit */
        case 0:
            ui->close_exit_radio_button->setChecked(true);
            break;

        /* hide */
        case 1:
            ui->close_hide_radio_button->setChecked(true);
            break;

        /* ask */
        case 2:
            ui->close_ask_radio_button->setChecked(true);
            break;

        default:
            qWarning() << "Unsupported state";
            break;
    }


    switch(settings->exit_on_connected.type)
    {
        /* ask */
        case 0:
            ui->exit_ask_radio_button->setChecked(true);
            break;

        /* disconnect */
        case 1:
            ui->exit_disconnect_radio_button->setChecked(true);
            break;

        /* force disconnect */
        case 2:
            ui->exit_force_disc_radio_button->setChecked(true);
            break;

        default:
            qWarning() << "Unsupported state";
            break;
    }

    ui->keep_log_button->setChecked(settings->log.lock_add?0:1);
    ui->max_log_size_spin_box->setValue(settings->log.max_log_size);
}


SettingsDialog::~SettingsDialog()
{
    delete ui;
}


void SettingsDialog::closeEvent(QCloseEvent *)
{
    deleteLater();
}


void SettingsDialog::on_buttonBox_accepted()
{
    if(ui->close_ask_radio_button->isChecked())
        settings->window_close.type = 2;
    else if(ui->close_hide_radio_button->isChecked())
        settings->window_close.type = 1;
    else if(ui->close_exit_radio_button->isChecked())
        settings->window_close.type = 0;

    if(ui->exit_ask_radio_button->isChecked())
        settings->exit_on_connected.type = 0;
    else if(ui->exit_disconnect_radio_button->isChecked())
        settings->exit_on_connected.type = 1;
    else if(ui->exit_force_disc_radio_button->isChecked())
        settings->exit_on_connected.type = 2;

    settings->log.lock_add = ui->keep_log_button->isChecked()?0:1;
    settings->log.max_log_size = ui->max_log_size_spin_box->value();

    close();
}


void SettingsDialog::on_buttonBox_rejected()
{
    close();
}
